// 筛选重组数组
/*
    @ data 传入的数组对象
    @ option 配置项
    @ mergerInventory: 合并实物库存
    @ inventoryOption: 实物库存option
    @ notAmountTips: 实重不能为空的判断
    @ notCategoryNameTips: 商品品名不能为空的判断
  */
export function filterOption(
  data,
  option,
  mergerInventory,
  inventoryOption,
  notAmountTips,
  notCategoryNameTips
) {
  let isNotAmount = false;
  let isNotCategoryName = false;
  let res = [];
  //第一次循环
  data.forEach(item => {
    let obj = {};

    if (!item.amount && notAmountTips) {
      isNotAmount = true;
    } else if (!item.categoryName && notCategoryNameTips) {
      isNotCategoryName = true;
    }
    //合并实物库存
    else if (mergerInventory) {
      //第二次循环
      data.forEach(i => {
        let { orderId } = i;
        if (!obj[orderId]) {
          obj[orderId] = {
            ...option(i),
            entityInventory: []
          };
        }
        i.entityInventory && i.entityInventory[0]
          ? obj[orderId].entityInventory.push({
              id: i.entityInventory[0].id,
              version: i.entityInventory[0].version,
              ...inventoryOption(i)
            })
          : obj[orderId].entityInventory.push({
              ...inventoryOption(i)
            });
      });
      res = Object.values(obj);
    } else {
      obj = { ...option(item) };
      res.push(obj);
    }
  });

  if (isNotAmount && notAmountTips) {
    this.$message.error("请输入实重!");
  }
  if (isNotCategoryName && notCategoryNameTips) {
    this.$message.error("请输入商品品名!");
  }
  return res;
}

//对象去重(仓库简称去重重组数组)
/*
    @ obj 构造obj
    @ arr 原数组
    @ property: 要对比的属性（property）
  */
export function objToHeavy(obj, arr, property) {
  let isExist = false;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i][property] === obj[property]) {
      isExist = true;
      break;
    }
  }
  return !isExist && arr.push(obj);
}
